<template>
  <el-card class="box-card">
    <div>
      <!-- 搜索框 -->
      <el-form
        ref="dataFrom"
        :inline="true"
        :model="dataFrom"
        class="demo-form-inline"
      >
        <el-form-item label="项目名称">
          <el-input
            v-model="dataFrom.name"
            clearable
            placeholder="项目名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            @click="getDataListByName"
          >查询
          </el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="dialogFormVisible = true">
            新增
          </el-button>
        </el-form-item>
      </el-form>
      <!-- 数据表单 -->
      <el-table ref="multipleTable" :data="dataList" border style="width: 100%">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="项目编号" prop="projectId" width="80">
        </el-table-column>
        <el-table-column label="项目名称" prop="project.name" width="150">
        </el-table-column>
        <el-table-column label="项目审核状态" prop="status" width="120">
        </el-table-column>
        <el-table-column label="项目创建人" prop="user.username" width="150">
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="180">
        </el-table-column>
        <el-table-column
          label="最近一次的修改时间"
          prop="updateTime"
          width="180"
        >
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="lookEdit(scope.$index, scope.row)"
            >
              查看
            </el-button>
            <el-button
              size="mini"
              type="success"
              @click="handleEdit(scope.$index, scope.row)"
            >
              审核
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 查看打开的界面 -->
      <el-dialog
        :model="formLook"
        :visible.sync="dialogFormVisibleLook"
        title="项目详情"
        width="50%"
      >
        <el-descriptions title="项目信息">
          <el-descriptions-item label="项目编号">
            {{ this.formLook.project.id }}
          </el-descriptions-item>
          <el-descriptions-item label="项目名称">
            {{ this.formLook.project.name }}
          </el-descriptions-item>
          <el-descriptions-item label="项目类型">
            {{ this.formLook.project.type }}
          </el-descriptions-item>
          <el-descriptions-item label="项目状态">
            {{ this.formLook.status }}
          </el-descriptions-item>

          <el-descriptions-item label="创建人">
            <el-tag size="small">{{ this.formLook.user.name }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="项目说明">{{
              this.formLook.project.remark
            }}
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions column:2>
          <el-descriptions-item label="申请资金">{{
              this.formLook.project.money
            }}
          </el-descriptions-item>
          <el-descriptions-item label="创建时间">{{
              this.formLook.createTime
            }}
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions>
          <el-descriptions-item label="最近一次修改时间" prop="updateTime">{{
              this.formLook.updateTime
            }}
          </el-descriptions-item>
        </el-descriptions>
        <el-divider></el-divider>
        <el-descriptions title="项目创建者信息">
          <el-descriptions-item label="账号">
            {{ this.formLook.user.username }}
          </el-descriptions-item
          >
          <el-descriptions-item label="姓名">
            <el-tag size="small">{{ this.formLook.user.name }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="邮箱">{{
              this.formLook.user.email
            }}
          </el-descriptions-item>
          <el-descriptions-item label="手机号码">{{
              this.formLook.user.mobile
            }}
          </el-descriptions-item>
          <el-descriptions-item label="账号状态">{{
              this.formLook.user.status === 0 ? "激活" : "禁用"
            }}
          </el-descriptions-item>
        </el-descriptions>
      </el-dialog>
      <!-- 新增按钮打开的页面 -->
      <el-dialog :visible.sync="dialogFormVisible" title="创建项目" width="35%">
        <el-form :model="form">
          <el-form-item label="项目名称" label-width="120px">
            <el-input
              v-model="form.project.name"
              autocomplete="off"
              placeholder="项目名称"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目内容" label-width="120px">
            <el-input
              v-model="form.project.content"
              autocomplete="off"
              placeholder="项目内容"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目类型" label-width="120px">
            <el-input
              v-model="form.project.type"
              autocomplete="off"
              placeholder="项目类型"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="申请资金" label-width="120px">
            <el-input
              v-model="form.project.money"
              autocomplete="off"
              placeholder="申请资金"
              style="width: 300px"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目说明" label-width="120px">
            <el-input
              v-model="form.project.remark"
              style="width: 300px"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel()">取 消</el-button>
          <el-button type="primary" @click="checkItemBeforeInsert()">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 审核按钮打开的页面 -->
      <el-dialog
        :model="formLook"
        :visible.sync="dialogFormVisibleChange"
        title="审核项目"
        width="50%"
      >
        <el-descriptions title="项目信息">
          <el-descriptions-item label="项目编号">
            {{ this.formLook.project.id }}
          </el-descriptions-item>
          <el-descriptions-item label="项目名称">
            {{ this.formLook.project.name }}
          </el-descriptions-item>
          <el-descriptions-item label="项目类型">
            {{ this.formLook.project.type }}
          </el-descriptions-item>
          <el-descriptions-item label="项目状态">{{
              this.formLook.status
            }}
          </el-descriptions-item>

          <el-descriptions-item label="创建人">
            <el-tag size="small">{{ this.formLook.user.name }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="项目说明">{{
              this.formLook.project.remark
            }}
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions column:2>
          <el-descriptions-item label="创建时间">{{
              this.formLook.createTime
            }}
          </el-descriptions-item>
          <el-descriptions-item label="申请资金">{{
              this.formLook.project.money
            }}
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions>
          <el-descriptions-item label="最近一次修改时间" prop="updateTime">{{
              this.formLook.updateTime
            }}
          </el-descriptions-item>
        </el-descriptions>
        <el-divider></el-divider>
        <!-- 审核结果和审核意见 -->
        <el-form :model="process">
          <el-form-item label="审核结果" label-width="120px">
            <el-select v-model="process.process" :placeholder="'未 审 核'">
              <el-option label="审 核 不 通 过" value="审核不通过"></el-option>
              <el-option label="审  核  通  过" value="审核通过"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="审核意见" label-width="120px">
            <el-input
              v-model="process.remark"
              autocomplete="off"
              placeholder="审核意见"
              style="width: 300px"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisibleChange = false">取 消</el-button>
          <el-button type="primary" @click="handleProcess()">提 交</el-button>
        </div>
      </el-dialog>
      <!-- 页号 -->
      <el-pagination
        :current-page="pageIndex"
        :page-size="pageSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        :total="totalPage"
        layout="total ,sizes,prev,pager,next,jumper"
        style="margin-top: 30px"
        @size-change="sizeChangeHandle"
        @current-change="CurrentChangeHandle"
      >
      </el-pagination>
    </div>
  </el-card>
</template>
<script>
export default {
  name: "ProcessProject",
  data() {
    return {
      userName: sessionStorage.getItem("username"),
      userId: sessionStorage.getItem("userId"),
      dataFrom: {
        name: "",
      },
      //新增
      form: {
        project: {
          name: "",
          content: "",
          type: "",
          money: "",
          mobile: "",
        },
        user: {
          username: "",
        },
      },
      //查看
      formLook: {
        project: {},
        user: {},
      },
      //编辑
      formChange: {
        id: "",
        project: {
          id: "",
          name: "",
          content: "",
          type: "",
          money: "",
          remark: "",
        },
        user: {
          username: "",
        },
      },
      //审核
      process: {
        process: "",
        remark: "",
        userId: "",
        projectId: "",
      },
      dataList: [], //页面展示的数据集合
      pageIndex: 1,
      pageSize: 5,
      totalPage: 0,
      begin: 0,
      end: this.pageSize - 1,
      dialogFormVisible: false, //默认关闭新建界面
      dialogFormVisibleChange: false, //默认关闭户界面
      dialogFormVisibleLook: false,
      dataListFrom: "getDataList",//当前数据来源于搜索还是全局
    };
  },
  methods: {
    //改变数组大小
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.SizeOrCurrentChangeAfter();
    },
    //改变当前页号
    CurrentChangeHandle(val) {
      this.pageIndex = val;
      this.SizeOrCurrentChangeAfter();
    },
    //在更改数组大小或者页号前，判断数据来源
    SizeOrCurrentChangeAfter() {
      if (this.dataListFrom === "getDataList")
        this.getDataList();
      else this.getDataListByName();
    },

    //检查创建项目时是否有未填项
    checkItemBeforeInsert() {
      if (this.form.project.money === "") {
        this.$confirm("当前申请资金为0,是否继续创建项目？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          this.handleInsert()
        }).catch(() => {
          this.$message({
            type: "info",
            message: "已取消创建项目",
          });
        });
      } else {
        this.handleInsert()
      }
    },
    //新增页面的确认键
    handleInsert() {
      this.$http.post("/item/insert", this.form).then(() => {
        this.dialogFormVisible = false; //关闭窗口
        this.getDataList();
      });
    },
    //审核页面的确认键
    handleProcess() {
      this.$http.post("/process/updateProcess", this.process).then(() => {
        this.dialogFormVisibleChange = false;
        this.getDataList();
      });
    },
    cutDataList(res) {
      const data = this.$cutPageDataList(res)
      this.dataList = data.dataList
      this.totalPage = data.totalPage
    },
    //查询所有数据
    getDataList() {
      this.dataListFrom = "getDataList";
      const pageInfo = this.$getPageInfo(this.pageSize, this.pageIndex)
      this.$http.post("/item/getList/Process", pageInfo).then((res) => {
        this.cutDataList(res);
      });
    },
    //查询单条数据
    getDataListByName() {
      const name = this.dataFrom.name
      if (name !== "") {
        const pageBo = this.$getPageInfo(this.pageSize, this.pageIndex)
        this.dataListFrom = "getDataListByName";
        this.$http.post("/item/getList/Process/search/" + name, pageBo).then((res) => {
          this.cutDataList(res);
        });
      } else this.getDataList();
    },
    //查看按钮
    lookEdit(index, item) {
      //回显数据
      this.formLook = item;
      //打开项目详细界面
      this.dialogFormVisibleLook = true;
    },
    //审核按钮
    handleEdit(index, item) {
      //回显数据
      this.formLook = item
      this.process = item.process;
      //打开界面
      this.dialogFormVisibleChange = true;
    },
    //关闭添加界面
    cancel() {
      this.dialogFormVisible = false;
    },
  },
  mounted() {
    this.getDataList();
  },
};
</script>
